package net.sf.jOpinionPoll.tag.component;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import org.acegisecurity.context.SecurityContextHolder;

public class UserInformationRenderer extends Renderer {

	@Override
	public void encodeBegin(FacesContext context,
	  UIComponent component)throws IOException
	{
		ResponseWriter writer = context.getResponseWriter();
		UIUserInformationComponent uiComponent = (UIUserInformationComponent)component;
        
        //span
		writer.startElement("span", component);
        
        //style
        if (component.getAttributes().get("style") != null)
            writer.writeAttribute("style", uiComponent.getStyle(), null);
        else if (uiComponent.getValueExpression("style") != null)
            writer.writeAttribute("style", uiComponent.getValueExpression("style").getValue(context.getELContext()).toString(), null);
            
        //class
        if (component.getAttributes().get("styleClass") != null)
            writer.writeAttribute("class", uiComponent.getStyleClass(), null);
        else if (uiComponent.getValueExpression("styleClass") != null)
            writer.writeAttribute("class", uiComponent.getValueExpression("lineClass").getValue(context.getELContext()).toString(), null);
        
        //valor - informacoes do usuario
        if (SecurityContextHolder.getContext().getAuthentication().getName() != null) {
        	writer.write(SecurityContextHolder.getContext().getAuthentication().getName());
        }
        
		writer.endElement("span");
        
		writer.flush();
	}
}